package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    private RoleService roleService;

    //完成用户 角色 和 权限的显示
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            Role role=roleService.findAll();
            System.out.println("role = " + role);
            return new Result(true,"完成用户和权限的显示回显成功",role);
        }catch (Exception e){
            return new Result(false,"完成用户和权限的显示回显失败");
        }

    }


    //完成role的数据添加
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] userIds,Integer[] permissionIds ){
        try{
            System.out.println("参数role = " + role);
            System.out.println("userIds = " + userIds);
            System.out.println("permissionIds = " + permissionIds);

            roleService.add(role,userIds,permissionIds);

            return new Result(true,"角色数据添加成功");
        }catch (Exception e){
            return new Result(false,"角色数据添加失败");
        }

    }

    //完成分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

          return  roleService.findPage(queryPageBean);


    }


    //根据id查询数据完成回显
    @RequestMapping("/findById")
    public Result findAById(Integer id){
        try{
            Role role=roleService.findAById(id);
            System.out.println("role = " + role);
            return new Result(true,"角色回显成功",role);
        }catch (Exception e){
            return new Result(false,"角色回显失败");
        }

    }


    //根据id查询给角色赋值用户的id
    @RequestMapping("/findUserAndRoleByUserId")
    public Result findUserAndRoleByUserId(Integer id){
        try{
            List<Integer> list =roleService.findUserAndRoleByUserId(id);
            System.out.println("list = " + list);
            return new Result(true,"完成用户回显成功",list);
        }catch (Exception e){
            return new Result(false,"完成用户回显失败");
        }

    }


    //根据id查询给角色赋值权限的id
    @RequestMapping("/findRoleAndPermissionByUserId")
    public Result findRoleAndPermissionByUserId(Integer id){
        try{
            List<Integer> list =roleService.findRoleAndPermissionByUserId(id);
            System.out.println("list = " + list);
            return new Result(true,"完成权限回显成功",list);
        }catch (Exception e){
            return new Result(false,"完成权限回显失败");
        }

    }


    //完成role的数据添加
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] userIds,Integer[] permissionIds ){
        try{
            System.out.println("参数role = " + role);
            System.out.println("userIds = " + userIds);
            System.out.println("permissionIds = " + permissionIds);

            roleService.edit(role,userIds,permissionIds);

            return new Result(true,"角色数据修改成功");
        }catch (Exception e){
            return new Result(false,"角色数据修改失败");
        }

    }

    //根据id删除数据
    @RequestMapping("/delete")
    public Result delete(Integer id ){
        try{
            roleService.delete(id);

            return new Result(true,"角色数据删除成功");
        }catch (Exception e){
            return new Result(false,"角色数据删除失败");
        }

    }






}
